home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / dev / lang / sbp3_1e.lzh / READSTR1.PL < prev    next >
Text File  |  1991-10-31  |  1KB  |  46 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5.  
  6. /* READSTR1.PL */
  7. /* First attempt at a "read string" predicate */
  8.  
  9. /*
  10.  * readstring(Result)
  11.  *   Lets the user type any number of characters, ending
  12.  *   with Return or New Line (Line Feed). Result is a list
  13.  *   of the ASCII codes of these characters.
  14.  *
  15.  *   In this version the backspace key does not delete the
  16.  *   previous character. Instead it lets the user retype
  17.  *   the entire line.
  18.  */
  19.  
  20. readstring(Result) :- put(62),            /* display '>' to prompt user */
  21.                       get0(FirstChar),
  22.                       readstring_aux(FirstChar,Result).
  23.  
  24. /* readstring_aux(Char,Result)
  25.  *   Char is the previous character, and
  26.  *   Result is a list (obtained recursively)
  27.  *   of it plus all subsequent characters.
  28.  */
  29.  
  30. readstring_aux(13,[]).  /* Return key */
  31. readstring_aux(10,[]).  /* New Line key */
  32.  
  33. readstring_aux(8,Result) :-      /* Backspace key */
  34.                     put(7),  /* beep */
  35.                     write(' <Start over>'),
  36.                     nl,
  37.                     readstring(Result).
  38.  
  39. readstring_aux(Char,[Char|Result]) :-
  40.                     Char \== 13,
  41.                     Char \== 10,
  42.                     Char \== 8,
  43.                     get0(NewChar),
  44.                     readstring_aux(NewChar,Result).
  45.  
  46.